//
//  SHNetworkTools.swift
//  三期微博1
//
//  Created by TownFish on 16/7/24.
//  Copyright © 2016年 TownFish. All rights reserved.
//

import UIKit
import AFNetworking
//请求方式的枚举
enum SHNetworkToolsType: String {
    case GET = "GET"
    case POST = "POST"
}


class SHNetworkTools: AFHTTPSessionManager {
    //全局访问点
    static let sharedTools: SHNetworkTools = {
        let tools = SHNetworkTools()
        //指定反序列化格式
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //网络请求支持get和post请求公共方法
    //请求公共方法
    /// - parameter type:       请求方式
    /// - parameter urlString:  请求url
    /// - parameter parameters: 请求参数
    /// - parameter success:    成功的闭包
    /// - parameter failure:    失败的闭包
    
    func request(type: SHNetworkToolsType, urlString: String, parameters: AnyObject?, success: (response: AnyObject?) ->(), failure: (error: NSError)->()) {
        //get请求
        if type == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response:res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
        }else{
            //post请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
            
        }
        
    }

}


//MARK: - 请求OAuth界面数据
extension SHNetworkTools{
    //请求userAccount
    func oauthLoadUserAccount(code: String, success: (response: AnyObject?) ->(), failure: (error: NSError)->()) {
        
        //请求urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let params = [
            "client_id": APPKEY,
            "client_secret": APPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": APPREDIRECT_URI
        ]
        request(SHNetworkToolsType.POST, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
    
    //请求用户信息数据
    func oauthLoadUserInfo(userAccount: SHUserAccount, success: (response: AnyObject?) ->(), failure: (error: NSError)->()) {
        //请求的urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        //请求参数
        let params = [
            "access_token": userAccount.access_token!,
            "uid": userAccount.uid!
        ]
        request(SHNetworkToolsType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
}


//MARK: - 请求首页微博界面数据
extension SHNetworkTools {
    func homeLoadData(success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        //请求urlSting
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //请求参数
        let params = ["access_token": SHUserAccountViewModel.sharedTools.accessToken!]
        //发送请求
        request(SHNetworkToolsType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}



